#ifndef DBUSMONITOR_H
#define DBUSMONITOR_H

#include <dbus/dbus.h>

#define DBM_MAX_RULE 1024
#define DBM_MAX_WATCHES 16
#define DBM_MAX_ADDR 256

typedef enum {
    DBMSTOP
}DBMCtrl;

typedef struct _DBusMonitor DBusMonitor;
typedef void (*DBusMonitorExecptionHandler) (void *);
typedef void (*DBusMonitorDispatchFunc) (DBusMonitor *);

/****************************************************
 *需要用户实现的MainLoop相关方法:
 *static dbus_bool_t add_watch(DBusWatch *w, void *data);
 *static void remove_watch(DBusWatch *w, void *data);
 *static void dispatch(DBusMonitor *m);
****************************************************/
DBusMonitor *dbmCreateWithType(DBusBusType type, 
                               DBusAddWatchFunction awf,
                               DBusRemoveWatchFunction rwf,
                               DBusMonitorDispatchFunc df);
DBusMonitor *dbmCreateWithAddr(const char *addr,
                               DBusAddWatchFunction awf,
                               DBusRemoveWatchFunction rwf,
                               DBusMonitorDispatchFunc df);
dbus_bool_t dbmInitConnection(DBusMonitor *m);
void dbmDestroy(DBusMonitor *m);
dbus_bool_t dbmStart(DBusMonitor *m);
void dbmStop(DBusMonitor *m);
dbus_bool_t dbmAddRule(DBusMonitor *m, const char *rule);
dbus_bool_t dbmRemoveRule(DBusMonitor *m, const char *rule);
dbus_bool_t dbmAddRules(DBusMonitor *m, const char *rules[]);
dbus_bool_t dbmAddHandler(DBusMonitor *m, DBusHandleMessageFunction handler,
                          void *usr_data, DBusFreeFunction fdf);
void dbmRemoveHandler(DBusMonitor *m, DBusHandleMessageFunction handler, 
                      void *usr_data);
void dbmStop(DBusMonitor *m);
void dbmSetExecptionHandler(DBusMonitor *m, DBusMonitorExecptionHandler eh,
                            void *usr_data);

DBusHandlerResult dbmDefaultHandler(DBusConnection *connection,
                                    DBusMessage *message, void *user_data);
int dbmGetCtrlFd(DBusMonitor *m);
int dbmGetWFd(DBusMonitor *m, int i); //获取第i个DBusWatche fd
DBusWatch *dbmGetDBW(DBusMonitor *m, int fd);
dbus_bool_t dbmAddDBW(DBusMonitor *m, DBusWatch *w);
dbus_bool_t dbmRemoveDBW(DBusMonitor *m, DBusWatch *w);
void dbmCloseConnection(DBusMonitor *m);
void dbmHandleDBusRead(DBusMonitor *m, int fd);
void dbmHandleDBusWrite(DBusMonitor *m, int fd);
void dbmHandleDBusExecption(DBusMonitor *m, int fd);
void dbmProcessDBusEvent(DBusMonitor *m, DBusWatch *w, unsigned flags);

#endif // DBUSMONITOR_H
